﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace Inaction.Util
{
    public static class ObjectUtil
    {
        #region Object Copy
        public static T CopyObject<T>(T source) where T : class
        {
            return CopyObject<T>(source, null, new List<string>());
        }
        public static T CopyObject<T>(T source, T target) where T : class
        {
            return CopyObject<T>(source, target, new List<string>());
        }
        public static T CopyObject<T>(T source, T target, string skipFiled) where T : class
        {
            return CopyObject<T>(source, target, new List<string>() { skipFiled });
        }
        public static T CopyObject<T>(T source, T target, List<string> skipFiledList) where T : class
        {
            if (target == null)
            {
                target = Activator.CreateInstance<T>();
            }
            PropertyInfo[] properties = source.GetType().GetProperties();
            foreach (var property in properties)
            {
                if (skipFiledList != null && skipFiledList.Count != 0 && skipFiledList.Contains(property.Name))
                    continue;

                if (property.CanWrite)
                {
                    property.SetValue(target, property.GetValue(source, null), null);
                }
            }
            return target;
        }

        #endregion
    }
}
